package com.ybkj.daijia.api.apirest.v4;

import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.DriverLogin;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverLoginService;
import com.ybkj.daijia.server.service.DriverService;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("ApiEmployProcessControllerV4")
@RequestMapping(value = "api/employ/v4/")
public class EmployProcessController {

    @Autowired
    private DriverService driverService;

    @Autowired
    private DriverLoginService driverLoginService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private SettingUtils settingUtils;


    @RequestMapping(value = "findEmploy")
    public @ResponseBody
    ResultTransport findEmploy(Long employId) {

        if (null == employId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        Driver driver = driverService.findOne(employId);

        if (null == driver) {
            return ResultTransport.getErrorBy(ErrorConstantV4.EMPLOY_NOT_EXISTS);
        }

        Company company = companyService.findOne(driver.getCompanyId());

        try {

            Setting setting = settingUtils.get();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("employId", driver.getId());
            map.put("employName", driver.getName());
            map.put("employRealName", driver.getRealname());
            map.put("employAccount", driver.getUsername());
            map.put("employPhone", driver.getPhone());
            map.put("employScore", driver.getStarLevel());
            map.put("employPhoto", setting.getWebUrl() + "/upload/driver/" + driver.getPhoto());
            map.put("companyId", company.getId());
            map.put("companyAbbreviation", company.getAbbreviation());
            map.put("employLat", driver.getLatitude());
            map.put("employLng", driver.getLongitude());
            map.put("employAddress", driver.getCurrentAddress());
            map.put("employStatus", driver.getStatus());
            if (null == driver.getJobStatus()) {
                map.put("employJobStatus", -1);
            } else {
                map.put("employJobStatus", driver.getJobStatus());
            }
            map.put("employJobType", driver.getDriverJobType());

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(map);

            return instance;

        } catch (Exception e) {
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }

    @RequestMapping(value = "findEmployByToken")
    public @ResponseBody
    ResultTransport findEmployByToken(String employToken) {

        if (StringUtils.isBlank(employToken)) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        DriverLogin driverLogin = driverLoginService.selectByToken(employToken);

        if (null == driverLogin) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_ERROR);
        }

        if (System.currentTimeMillis() >= driverLogin.getDeathDate().getTime()) {
            return ResultTransport.getErrorBy(ErrorConstantV4.TOKEN_TIME_ERROR);
        }

        Driver driver = driverService.findOne(driverLogin.getDriverId());

        if (null == driver) {
            return ResultTransport.getErrorBy(ErrorConstantV4.EMPLOY_NOT_EXISTS);
        }

        Company company = companyService.findOne(driver.getCompanyId());

        try {

            Setting setting = settingUtils.get();

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("employId", driver.getId());
            map.put("employName", driver.getName());
            map.put("employRealName", driver.getRealname());
            map.put("employAccount", driver.getUsername());
            map.put("employPhone", driver.getPhone());
            map.put("employScore", driver.getStarLevel());
            map.put("employPhoto", setting.getWebUrl() + "/upload/driver/" + driver.getPhoto());
            map.put("companyId", company.getId());
            map.put("companyAbbreviation", company.getAbbreviation());
            map.put("employLat", driver.getLatitude());
            map.put("employLng", driver.getLongitude());
            map.put("employAddress", driver.getCurrentAddress());
            map.put("employStatus", driver.getStatus());
            map.put("detailedJobType", driver.getDetailedJobType());

            if (null == driver.getJobStatus()) {
                map.put("employJobStatus", -1);
            } else {
                map.put("employJobStatus", driver.getJobStatus());
            }
            map.put("employJobType", driver.getDriverJobType());

            map.put("errandserviceType", driver.getErrandserviceType());
            map.put("platNo", driver.getPlatNo());
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(map);

            return instance;

        } catch (Exception e) {
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }

    }

}
